package com.zr.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import com.zr.dao.RouteDao;
import com.zr.model.TDistributionpoint;
import com.zr.model.TRoute;
import net.sf.json.JSONArray;
@Repository("routeDao")
public class RouteDaoImpl<T> extends BaseDaOImpl<T> implements RouteDao<T> {

	@Override
	public long showRouteCount() {
		// TODO Auto-generated method stub
		//查询路线总数的hql语句
		String hql = "select count(*) from TRoute";
		//将查出来的数据放在list中
		List<Object> obj = this.getCurrentSession().createQuery(hql).list();
		//获取数据
		return (long) obj.get(0);
	}

	@Override
	public List<TRoute> showRouteMsg(int page, int rows) {
		// TODO Auto-generated method stub
	  //查询路线信息的sql语句
	  String sql = "SELECT rid,rname,(SELECT dpname from t_distributionpoint p where t.rstart=p.dpid) rstart,(SELECT dpname from t_distributionpoint p where t.rend=p.dpid) rend from t_route t limit :start,:scount";
	  //将查出来的数据转成map
	  Query query=this.getCurrentSession().createSQLQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
	  //指定当前分页查询的起始位置和行数
      query.setInteger("start",(page-1)*rows);
	  query.setInteger("scount",rows);
	  //将Query对象query转换成集合,并且把该集合赋值给一个新的集合
	  List<TRoute> routes = query.list();
	  return routes;
	}

	@Override
	public JSONArray findDpnameMsg() {
		// TODO Auto-generated method stub
		//声明一个map数据集合
		Map<String, String> map = new HashMap<String,String>();
		//声明一个jso数组
		JSONArray jso=new JSONArray();
		//查询配送点名称信息的hql语句
		String hql="from TDistributionpoint";
		//查询hql语句
		Query query=this.getCurrentSession().createQuery(hql);
		//将查询出来的hql数据放入list中
		List<TDistributionpoint> tdbp = query.list();
		for (TDistributionpoint tDistributionpoint : tdbp) {	
			//获得配送点的id和名字
			String Dpid = String.valueOf(tDistributionpoint.getDpid());
			String Dpname = tDistributionpoint.getDpname();
			//将配送点id和名字放入map集合中
			map.put("id", Dpid);
			map.put("text", Dpname);
			//将这个集合放入jso数组中
			jso.add(map);
		}
		//返回jso数组
		return jso;
	}

	@Override
	public boolean addRouteMsg(String rname, String rstart, String rend) {
		// TODO Auto-generated method stub
		boolean flag =false;
		TRoute route = new TRoute();
		route.setRname(rname);
		route.setRstart(rstart);
		route.setRend(rend);
		this.getCurrentSession().save(route);
		return flag;
	}

	@Override
	public void deleteRouteMsg(int rid) {
		// TODO Auto-generated method stub
		TRoute route = (TRoute) this.getCurrentSession().get(TRoute.class, Integer.valueOf(rid));
		this.getCurrentSession().delete(route);
	}

	@Override
	public void updateRouteMsg(int rid, String rname, String rstart, String rend) {
		// TODO Auto-generated method stub
		TRoute td = (TRoute) this.getCurrentSession().get(TRoute.class, Integer.valueOf(rid));
		td.setRname(rname);
		td.setRstart(rstart);
		td.setRend(rend);
		this.getCurrentSession().update(td);
	}
}
